package village.rainbow.slipin.datamodel;

import java.util.ArrayList;
import java.util.LinkedHashMap;
public class DataTable extends ArrayList<DataRow> {

	/**
	 * C# 의 Datatable과 같은 개념
	 * DataRow를 Template(Collection Type)으로 갖는다.
	 */
	private static final long serialVersionUID = -9111574161917126340L;
	
	public DataTable(){}
	public DataTable(ArrayList<LinkedHashMap<String, Object>> data)
	{
		if (data == null)
			return;
		for (int i = 0; i < data.size(); i++)
			add(new DataRow(data.get(i)));
	}
	
	public void replace(DataRow row, int index){
		this.remove(index);
		this.add(index, row);
	}
	
	@Override
	public Object clone() {
		
		DataTable dt = new DataTable();
		for(DataRow dr : this) {
			dt.add((DataRow)dr.clone());
		}
		
		return dt;
	}
	
	public DataRow getSpecificDataRow(String key1, String value1, String key2, String value2){
		DataRow targetRow = new DataRow();
		for(DataRow row : this){
			if(row.getString(key1, "").equals(value1) && 
					row.getString(key2, "").equals(value2) )
				targetRow = row;
		}
		return targetRow;
	}
	
	public DataRow getSpecificDataRow(String key, String value){
		DataRow targetRow = new DataRow();
		for(DataRow row : this){
			if(row.getString(key, "").equals(value))
				targetRow = row;
		}
		return targetRow;
	}
	
	public ArrayList<String> getSpecificStringArray(String key){
		ArrayList<String> list = new ArrayList<String>();
		for(int i=0; i<this.size(); i++){
			DataRow row = this.get(i);
			list.add(row.getString(key, ""));
		}
		list.trimToSize();
		return list;
	}
	
	public String getString()
	{
		StringBuilder builder = new StringBuilder(1024);
		
		for (int i = 0; i < size(); i++)
			builder.append(get(i).getString() + "\n");
		builder.append("Count : " + size());
		
		return builder.toString();
	}
	public void setData(ArrayList<LinkedHashMap<String, Object>> data)
	{
		if (data == null)
			return;
		for (int i = 0; i < data.size(); i++)
		{
			if (data.get(i) instanceof DataRow)
				add((DataRow)data.get(i));
		}
	}
	
	public ArrayList<LinkedHashMap<String, Object>> getDataViaArrayList(){
		ArrayList<LinkedHashMap<String, Object>> list = new ArrayList<LinkedHashMap<String, Object>>();
		list.addAll(this);
		return list;
	}
	
	public void recalcSortOrdeR(String key) {
		for(int i=0; i<this.size(); i++){
			this.get(i).put(key, i);
		}
	}
}
